package com.boc.ewr.business.service.impl;

import com.boc.ewr.bcimp.entity.*;
import com.boc.ewr.bcimp.service.*;
import com.boc.ewr.business.entity.CaCollList;
import com.boc.ewr.business.mapper.CaCollListMapper;
import com.boc.ewr.business.service.ICaCollListService;
import com.boc.ewr.util.file.enumeration.CaAccountStatusEnum;
import com.boc.ewr.util.file.enumeration.SendChannelEnum;
import org.jeecg.common.mybatis.query.LambdaQueryWrapperX;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: 催收名单
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class CaCollListServiceImpl extends ServiceImpl<CaCollListMapper, CaCollList> implements ICaCollListService {

    // 余额对证单
    @Autowired
    private ICacService cacService;

    // 对公账户信息
    @Autowired
    private IBusAcctInfoService busAcctInfoService;

    // 特殊不对账清单
    @Autowired
    private ICaUnpriaccListService caUnpriaccListService;

    // 未回清单
    @Autowired
    private ICaUnrtnListService caUnrtnListService;


    /**
     * 生成催收名单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        /*
        1.以CAC文件为主数据，先去掉CAC中
        “对账联系人固定电话、对账联系人手机、财务负责人姓名、财务负责人固定电话、财务负责人手机”
        均为空或不足位数的数据，
        2.再去掉“对公账户信息”中的已关户、不动户
        3.以及特殊不对账账户的数据
        4.筛选出有“未回清单”中网银对账渠道的账单数据。
        */
        //清空当前记录

        //生成数据
        // 筛选均为空
        LambdaQueryWrapperX<Cac> cacWrapperX = new LambdaQueryWrapperX<>();
        cacWrapperX.isNotNull(Cac::getLinkerTel).or()
                .isNotNull(Cac::getLinkerMobile).or()
                .isNotNull(Cac::getFinacePrincipalTel).or()
                .isNotNull(Cac::getFinacePrincipalName).or()
                .isNotNull(Cac::getFinacePrincipalMobile);

        List<Cac> baseList = cacService.list(cacWrapperX);
        // 判断不足位数
        List<Cac> resultList = this.judgeLength(baseList);
        // 获取 对公账户信息 已关户、不动户、不对账账户
        LambdaQueryWrapperX<BusAcctInfo> acctSmallUnimpWrapperX = new LambdaQueryWrapperX<BusAcctInfo>();
        List<String> acctStaList = new ArrayList<>();
        acctStaList.add(CaAccountStatusEnum.ACC_UNCHANGED.getIndex());
        acctStaList.add(CaAccountStatusEnum.ACC_SHUTDOWN.getIndex());
        acctStaList.add(CaAccountStatusEnum.ACC_UNPRI.getIndex());
        acctSmallUnimpWrapperX.in(BusAcctInfo::getAcctSta, acctStaList);
        List<BusAcctInfo> busAcctInfos = busAcctInfoService.list(acctSmallUnimpWrapperX);
        // 获取特殊不对账账户数据
        List<CaUnpriaccList> caUnpriaccLists = caUnpriaccListService.list();
        // 去掉数据
        List<Cac> elmtedCacList = this.eliminateSmallAcct(resultList, busAcctInfos, caUnpriaccLists);

        // 筛选“未回清单”中网银对账渠道的账单数据。
        LambdaQueryWrapperX<CaUnrtnList> caUnrtnListWrapperX = new LambdaQueryWrapperX<CaUnrtnList>();
        caUnrtnListWrapperX.eq(CaUnrtnList::getSendChannel, SendChannelEnum.ONLINE.getIndex());

        List<CaUnrtnList> caUnrtnLists = caUnrtnListService.list(caUnrtnListWrapperX);

        List<CaCollList> caCollList = this.getCaCollList(elmtedCacList, caUnrtnLists);
        // 保存数据
        return this.saveBatch(caCollList);
    }


    public List<CaCollList> getCaCollList(List<Cac> elmtedCacList, List<CaUnrtnList> caUnrtnLists) {
        // todo 缺少样本数据，待测试
        List<CaCollList> resultList = new ArrayList<>();
        for (CaUnrtnList caUnrtnList : caUnrtnLists) {
            String protocolNo = caUnrtnList.getProtocolNo();
            CaCollList caCollList = new CaCollList();
            caCollList.setCaMon(caUnrtnList.getCaMon());
            caCollList.setCusmNm(caCollList.getCusmNm());
            caCollList.setBranchNm(caCollList.getBranchNm());
            for (Cac cac : elmtedCacList) {
                if (cac.getCaProtocolNo().equals(protocolNo)){
                    String cusmNoName = cac.getCusmNo()+"-"+cac.getLinkerName();
                    caCollList.setCusmNoName(cusmNoName);
                    caCollList.setLinkerTel(cac.getLinkerTel());
                    caCollList.setFinacePrincipalMobile(cac.getFinacePrincipalMobile());
                    caCollList.setFinacePrincipalTel(cac.getFinacePrincipalTel());
                    resultList.add(caCollList);
                }
            }
        }
        return  resultList;
    }

    public List<Cac> eliminateSmallAcct(List<Cac> cacList, List<BusAcctInfo> busAcctInfos, List<CaUnpriaccList> caUnpriaccLists) {
        Set<String> acctRemoveSet = new HashSet<>();
        for (BusAcctInfo busAcctInfo : busAcctInfos) {
            acctRemoveSet.add(busAcctInfo.getAccNo());
        }
        for (CaUnpriaccList caUnpriaccList : caUnpriaccLists) {
            acctRemoveSet.add(caUnpriaccList.getAcctTxtNo());
        }

        List<Cac> resultList = new ArrayList<>();
        for (Cac cac : cacList) {
            if (acctRemoveSet.contains(cac.getAccNo())) {
                resultList.add(cac);
            }
        }
        return resultList;
    }

    public List<Cac> judgeLength(List<Cac> baseList) {
        List<Cac> resultList = new ArrayList<>();
        int telLength = 8;
        int mobLength = 11;
        for (Cac cac : baseList) {
            if (cac.getLinkerTel().length() < telLength) {
                continue;
            }
            if (cac.getLinkerMobile().length() < mobLength) {
                continue;
            }
            if (cac.getFinacePrincipalTel().length() < telLength) {
                continue;
            }
            if (cac.getFinacePrincipalMobile().length() < mobLength) {
                continue;
            }
            resultList.add(cac);
        }
        return resultList;
    }

}
